package com.yonyou.pmclouds.reason.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonProgramConsts;
import com.yonyou.pmclouds.offline.aspect.Offline;
import com.yonyou.pmclouds.offline.entity.OffLineDataKeyConst;
import com.yonyou.pmclouds.offline.entity.OffLineInvocationContext;
import com.yonyou.pmclouds.offline.rmiitf.IOffLineDataQueryService;
import com.yonyou.pmclouds.reason.entity.ProblemReasonRefVO;
import com.yonyou.pmclouds.reason.entity.ProblemReasonVO;
import com.yonyou.pmclouds.reason.mapper.ProblemReasonMapper;
import com.yonyou.pmclouds.reason.rmiitf.ProblemReasonQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service(interfaceClass = ProblemReasonQuery.class, version = ApplicationConsts.APPLICATION_VERSION)
@Offline(dataFrom = OffLineDataKeyConst.DATA_PROBLEM_REASON)
public class ProblemReasonQueryImpl implements ProblemReasonQuery, IOffLineDataQueryService {
    @Autowired
    private ProblemReasonMapper mapper;

    @Override
    public ProblemReasonVO[] queryReasonByTenant(String tenantid, byte enableStatus, byte category) throws BusinessException {
        try {
            return mapper.queryReasonsOfTenant(tenantid, enableStatus, category);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new BusinessException();
        }
    }

    @Override
    public ProblemReasonRefVO[] queryReasonRefByTenant(String tenantid, Byte category) throws BusinessException {
        try {
            return mapper.queryReasonsRefOfTenant(tenantid, category);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new BusinessException();
        }
    }

    @Override
    public ProblemReasonVO getById(String id) throws BusinessException {
        try {
            return mapper.getById(id);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new BusinessException();
        }
    }

    @Override
    public Map<String, ProblemReasonRefVO> queryRefs(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        //强制截取长度，避免insql可能的问题
        if (ids.length > CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE) {
            ids = Arrays.copyOf(ids, CommonProgramConsts.DEFAULT_MAX_QUERY_SIZE);
        }
        // 这里不作权限控制
        ProblemReasonRefVO[] refs = null;
        try {
            refs = mapper.queryRefVOByIds(ids);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new BusinessException();
        }
        if (ArrayUtils.isEmpty(refs)) {
            return new HashMap<>();
        }
        Map<String, ProblemReasonRefVO> result = new HashMap<>(refs.length);
        for (ProblemReasonRefVO ref : refs) {
            result.put(ref.getPkReason(), ref);
        }
        return result;
    }

    @Override
    public Map<String, String> queryNameByIds(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids)) {
            return null;
        }
        ProblemReasonRefVO[] refVOS = mapper.queryRefVOByIds(ids);
        if (ArrayUtils.isEmpty(refVOS)) {
            return null;
        }
        Map<String, String> result = new HashMap<>(refVOS.length);
        for (ProblemReasonRefVO vo : refVOS
                ) {
            result.put(vo.getPkReason(), vo.getSname());
        }
        return result;
    }

    @Override
    public ProblemReasonVO[] queryIdByNames(String[] names, String tenantId) throws BusinessException {
        if (ArrayUtils.isEmpty(names)) {
            return null;
        }
        return mapper.queryIdByNames(names, tenantId);
    }

    @Override
    public Object queryOffLineData(OffLineInvocationContext invocationContext) throws BusinessException {

        ProblemReasonRefVO[] problemReasonRefVOS = queryReasonRefByTenant(invocationContext.getTenantId(), null);

        return problemReasonRefVOS;
    }
}
